/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdbool.h>

#define M_PI 3.1415

#define SYSTEM_CORE_CLOCK			240000000


/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define ENC_A_Pin GPIO_PIN_6
#define ENC_A_GPIO_Port GPIOA
#define ENC_B_Pin GPIO_PIN_7
#define ENC_B_GPIO_Port GPIOA
#define ENC_C_Pin GPIO_PIN_12
#define ENC_C_GPIO_Port GPIOD
#define ENC_C_EXTI_IRQn EXTI15_10_IRQn
#define EN_GATE_Pin GPIO_PIN_13
#define EN_GATE_GPIO_Port GPIOD
#define DRV8301_CS_Pin GPIO_PIN_9
#define DRV8301_CS_GPIO_Port GPIOC
#define DRV8301_SCK_Pin GPIO_PIN_10
#define DRV8301_SCK_GPIO_Port GPIOC
#define DRV8301_MISO_Pin GPIO_PIN_11
#define DRV8301_MISO_GPIO_Port GPIOC
#define DRV8301_MOSI_Pin GPIO_PIN_12
#define DRV8301_MOSI_GPIO_Port GPIOC
#define FAULT_Pin GPIO_PIN_7
#define FAULT_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */
#define MAX_THREADS_MONITOR		10
#define VIN_R1					39000.0
#define VIN_R2					2200.0
//#define HW_DEAD_TIME_NSEC				360.0	// Dead time

	
static volatile uint32_t feed_counter[MAX_THREADS_MONITOR];

extern int new_t;
void buffer_append_uint16(uint8_t* buffer, uint16_t number, int32_t *index);
void comm_can_set_current(int controller_id, float current);
float GET_INPUT_VOLTAGE(void);
float GET_INPUT_MOS_VOLTAGE(int k);
int GET_CURRENT(int t);
void Mode_Change(float t,float current,float duty,float speed,float FW);
uint8_t conf_general_calculate_deadtime(float deadtime_ns, float core_clock_freq);
void tim5_delay_ms(uint16_t nms);
int32_t buffer_get_int32(const uint8_t *buffer, int32_t *index);
float buffer_get_float32(const uint8_t *buffer, float scale, int32_t *index);
void buffer_append_int32_le(uint8_t* buffer, int32_t number, int32_t *index);


extern float Iq_set;
extern float Id_set;
extern float Current1;
extern float Current2;
extern float Current3;
extern float Read_vd;
extern float Read_vq;
extern float Read_v_alpha;
extern float Read_v_beta;
extern float Read_phase;
extern float Read_Iq;
extern float Read_Id;
extern float voltage_a;
extern float voltage_b;
extern float voltage_c;
extern float Duty_now;
extern float Speed_now;
extern float Dead_Time;
extern float add_count;

//extern mc_configuration config;
//static volatile virtual_motor_t virtual_motor;

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
